<?php
/**
 * @Project: None
 * @FileName: RoleAttr.php
 * @FilePath: D:\thinkphp6\tp\app\controller\RoleAttr.php
 * @Author: zishuiliufeng
 * @Date:   2021-01-28 17:21:49
 * @Last Modified by:   zishuiliufeng
 * @Last Modified time: 2021-02-06 17:39:17
 */
namespace app\controller;
use think\Facade\Db;
use think\facade\Request;
use app\BaseController;
use app\Response;
use app\model\RoleAttribute as RAttr;
use app\model\GameAttribute as GAttr;
use app\model\RoleInfo;

class RoleAttr extends BaseController{
    /**
     * [gameList 获取游戏属性列表]
     * @return [type] [description]
     */
    public function roleAttrList(){
        //静态获取参数
        //$param = Request::param();
        $param = $this->request->param();
        $page = isset($param['page'])?(int)$param['page']:1;
        $perpage = isset($param['perpage'])?(int)$param['perpage']:10;

        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;

        $name = isset($param['name'])?(string)$param['name']:'';

        //筛选条件数组
        $where = [];

        if(!empty($name)){
            $where[] = [
                ['name', 'like', '%'.$name.'%']
            ];
        }

        // 查询数据 
        $list = Db::name('role_attribute')->where('role_id',$role_id)->where($where)->order('id' ,'asc')->paginate($perpage);
        //当前页数
        $currentPage = $list->currentPage();
        // 获取总记录数
        $total = $list->total();
        //最后页数
        $lastPage = $list->lastPage();
        //每页显示数
        $listRows = $list->listRows();
        //数据
        $data = $list->items();
        
        $result = array(
            'currentPage' => $currentPage,
            'total' => $total,
            'lastPage' => $lastPage,
            'listRows' => $listRows,
            'content' => $data
        );

        
        Response::show(200,'ok',$result);
    }

    /**
     * [getList 获取人物属性列表，不分页]
     * @return [json] [description]
     */
    public function getList()
    {
        $param = $this->request->param();
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;

        if($role_id < 1){
            $result = array('role_id'=>$role_id);
            Response::show(201,'参数错误',$result);
        }

        $role_attr = RAttr::where('role_id',$role_id)->order('id', 'asc')->select();

        $result = array();
        if(!$role_attr->isEmpty()){
            foreach ($role_attr as $key => $value) {
                $result[] = array(
                    'id' => $value->id,
                    'game_id' => $value->game_id,
                    'role_id' => $value->role_id,
                    'name' => $value->name,
                    'en_name' => $value->en_name,
                    'introduction' => $value->introduction,
                    'game_attribute_id' => $value->game_attribute_id
                    );
            }
        }
        Response::show(200,'ok',$result);
    }

    /**
     * [edit 游戏属性编辑]
     * @return [type] [description]
     */
    public function edit(){
        $param = Request::param();
        $id = isset($param['id'])?(int)$param['id']:0;
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;

        if($id < 1 || $role_id < 1){
            $result = array('id'=>$id,'role_id'=>$role_id,'game_id'=>$game_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //人物属性名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'人物属性名称与英文简称不能为空');
        }

        //同一游戏内人物属性不能重复
        $repeat = RAttr::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty() && $repeat->id != $id){
            Response::show(300,'人物属性已存在');
        }

        //同一游戏内人物属性英文简称也不能重复
        $repeat_en_name = RAttr::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty() && $repeat_en_name->id != $id){
            Response::show(300,'游戏属性英文简称已存在');
        }

        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';

        //根据id查找数据，并判断数据是否存在
        $role = RAttr::where('id',$id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'数据不存在');
        }
        //存在则修改
        $role->name = $name;
        $role->en_name = $en_name;
        $role->introduction = $introduction;
        $role->update_time = date('Y-m-d H:i:s');
        $role->save();
        Response::show(200,'修改成功');
    }
    /**
     * [addAttr 增加游戏属性]
     */
    public function addAttr(){
        $param = Request::param();
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        $game_attribute_id = isset($param['game_attribute_id'])?(int)$param['game_attribute_id']:0;

        if($game_id < 1 || $role_id < 1){
            $result = array('game_id'=>$game_id,'role_id'=>$role_id);
            Response::show(201,'参数错误',$result);
        }

        $name = isset($param['name'])?(string)$param['name']:'';
        $en_name = isset($param['en_name'])?(string)$param['en_name']:'';
        //人物属性名称、英文简称不能为空
        if(empty($name) || empty($en_name)){
            Response::show(301,'人物属性名称与英文简称不能为空');
        }

        //同一游戏内人物属性不能重复
        $repeat = RAttr::where('name',$name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat->isEmpty()){
            Response::show(300,'人物属性已存在');
        }

        //同一游戏内游戏属性英文简称也不能重复
        $repeat_en_name = RAttr::where('en_name',$en_name)->where('game_id',$game_id)->findOrEmpty();
        if(!$repeat_en_name->isEmpty()){
            Response::show(300,'人物属性英文简称已存在');
        }

        $introduction = isset($param['introduction'])?(string)$param['introduction']:'';

        $attr = new RAttr;
        $attr->game_id = $game_id;
        $attr->role_id = $role_id;
        $attr->game_attribute_id = $game_attribute_id;
        $attr->name = $name;
        $attr->en_name = $en_name;
        $attr->introduction = $introduction;
        $attr->create_time = date('Y-m-d H:i:s');
        $attr->update_time = date('Y-m-d H:i:s');
        $attr->save();
        //获取自增ID
        $id = $attr->id;
        Response::show(200,'新增成功',$id);
    }

    /**
     * [delRoleAttr 删除人物属性]
     * @return [type] [description]
     */
    public function delRoleAttr(){
        $param = Request::param();
        $role_attribute_id = isset($param['role_attribute_id'])?(int)$param['role_attribute_id']:0;

        $del = RAttr::where('id',$role_attribute_id)->delete();
        if($del > 0){
            $code = 200;
            $msg = '删除成功';
        }else{
            $code = 204;
            $msg = '删除失败';
        }
        Response::show($code,$msg);
    }

    /**
     * [combine 人物属性组合]
     * @return [type] [description]
     */
    public function combine(){
        $param = Request::param();
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;
        $game_attribute_id = isset($param['game_attribute_id'])?(int)$param['game_attribute_id']:0;

        if($game_id < 1 || $role_id < 1 || $game_attribute_id < 1){
            $result = array('game_id'=>$game_id,'role_id'=>$role_id,'game_attribute_id'=>$game_attribute_id);
            Response::show(201,'参数错误',$result);
        }

        //获取人物信息
        $role = RoleInfo::where('id',$role_id)->findOrEmpty();
        if($role->isEmpty()){
            Response::show(404,'角色不存在');
        }
        $role_name = $role->name;
        $role_en_name = $role->en_name;
        $role_introduction = $role->introduction;

        //获取游戏属性信息
        $game_attr = GAttr::where('id',$game_attribute_id)->findOrEmpty();
        if($game_attr->isEmpty()){
            Response::show(404,'游戏属性不存在');
        }
        $game_attr_name = $game_attr->name;
        $game_attr_en_name = $game_attr->en_name;
        $game_attr_introduction = $game_attr->introduction;

        //组合信息
        $role_attribute_name = $role_name.'的'.$game_attr_name;
        $role_attribute_en_name = $role_en_name.'_'.$game_attr_en_name;
        $role_attribute_introduction = $role_name.'的'.$role_attribute_name.'事件';

        //人物属性保存
        $role_attr = new RAttr;
        $role_attr->game_id = $game_id;
        $role_attr->role_id = $role_id;
        $role_attr->game_attribute_id = $game_attribute_id;
        $role_attr->name = $role_attribute_name;
        $role_attr->en_name = $role_attribute_en_name;
        $role_attr->introduction = $role_attribute_introduction;
        $role_attr->create_time = date('Y-m-d H:i:s');
        $role_attr->update_time = date('Y-m-d H:i:s');
        $role_attr->save();
        //获取自增ID
        $id = $role_attr->id;
        $result = array(
            'role_attribute_id' => $id,
            'game_id' => $game_id,
            'role_id' => $role_id,
            'name' => $role_attribute_name,
            'en_name' => $role_attribute_en_name,
            'introduction' => $role_attribute_introduction,
            'game_attribute_id' => $game_attribute_id
            );
        Response::show(200,'新增成功',$result);
    }

    /**
     * [compareAttrList 根据game_id获取表game_attribute游戏属性，根据role_id获取表role_attribute人物属性，根据role_attribute.game_attribute_id与game_attribute.id比较获取两者的交集和差集]
     * @return [json] [游戏属性与人物属性]
     */
    public function compareAttrList(){
        $param = Request::param();
        $game_id = isset($param['game_id'])?(int)$param['game_id']:0;
        $role_id = isset($param['role_id'])?(int)$param['role_id']:0;

        if($game_id < 1 || $role_id < 1){
            $result = array('game_id'=>$game_id,'role_id'=>$role_id);
            Response::show(201,'参数错误',$result);
        }

        //人物属性信息列表
        $role_attr_list = array();
        //人物属性中使用过的游戏属性列表id
        $used_game_attr_id = array();

        //获取人物属性
        $role_attr = RAttr::where('role_id',$role_id)->select();
        if(!$role_attr->isEmpty()){
            foreach ($role_attr as $key => $value) {
                $role_attr_list[] = array(
                    'role_attribute_id' => $value->id,
                    'game_id' => $value->game_id,
                    'role_id' => $value->role_id,
                    'name' => $value->name,
                    'en_name' => $value->en_name,
                    'introduction' => $value->introduction,
                    'game_attribute_id' => $value->game_attribute_id
                    );
                //判断游戏属性是否已使用
                if(!in_array($value->game_attribute_id, $used_game_attr_id)){
                    $used_game_attr_id[] = $value->game_attribute_id;
                }
            }
        }

        //游戏属性信息列表
        $game_attr_list = array();

        //获取游戏属性
        $game_attr = GAttr::where('game_id',$game_id)->select();

        if(!$game_attr->isEmpty()){
            foreach ($game_attr as $k => $v) {
                //判断游戏属性是否已使用过is_used
                if(in_array($v->id,$used_game_attr_id)){
                    $game_attr_list[] = array(
                        'game_attribute_id' => $v->id,
                        'game_id' => $v->game_id,
                        'name' => $v->name,
                        'en_name' => $v->en_name,
                        'introduction' => $v->introduction,
                        'is_used' => true
                    );
                }else{
                    $game_attr_list[] = array(
                        'game_attribute_id' => $v->id,
                        'game_id' => $v->game_id,
                        'name' => $v->name,
                        'en_name' => $v->en_name,
                        'introduction' => $v->introduction,
                        'is_used' => false
                    );
                }
                
            }
        }

        $result = array(
            'game_attr_list' => $game_attr_list,
            'role_attr_list' => $role_attr_list
            );
        Response::show(200,'游戏属性与人物属性',$result);
    }
}